/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.saen.nextincloud.dao;

import co.saen.nextincloud.hibernate.HibernateUtil;
import co.saen.nextincloud.model.Turns;
import co.saen.nextincloud.model.TurnsReasons;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.hibernate.Session;

/**
 *
 * @author Felipe Rolvar
 */
public class TurnsDAO {

    private Turns turn;
    private List<Turns> turns;
//    Date nowDate = new Date();
    Calendar today = Calendar.getInstance();
    String nowDate = Integer.toString(today.get(Calendar.YEAR)) + "-" + Integer.toString(today.get(Calendar.MONTH) + 1) + "-" + Integer.toString(today.get(Calendar.DATE));

    public void setTurn(Turns turn) {
        this.turn = turn;
    }

    public Turns getTurn() {
        if (turn == null) {
            turn = new Turns();
        }
        return turn;
    }

    public TurnsDAO() {
    }

    public Turns createTurn(Turns turn) {
        turn.setTrnNmbr(consecTurn());
//        turn.setTrnStat(Boolean.FALSE);
        turn.setTrnDate(mysqlDate(nowDate));

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(turn);
            session.beginTransaction().commit();
        } catch (Exception e) {
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
            HibernateUtil.getSessionFactory().close();
        }
        return searchTurnById(turn.getTrnNmbr());
    }

    public String updateTurn(Turns turn) {

        String msg = "";
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(turn);
            session.beginTransaction().commit();
            msg = "ok";
        } catch (Exception e) {
            msg = "Error al Actualizar Turno: " + e.getMessage();
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
        }

        return msg;

    }

    public String deleteTurn(Turns turn) {

        String msg = "";
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(turn);
            session.beginTransaction().commit();
            msg = "ok";
        } catch (Exception e) {
            msg = "Error al Eliminar Turno: " + e.getMessage();
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
        }

        return msg;

    }

    public List<Turns> pendingTurns() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("FROM Turns WHERE trnDate = '" + nowDate + "' AND trnStat = 0 ORDER BY trnId ASC").list();
    }

    public List<Turns> allTurns() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("FROM Turns").list();
    }

    public Integer consecTurn() {
        Integer next;
        Session session = HibernateUtil.getSessionFactory().openSession();
        turns = session.createQuery("FROM Turns WHERE trnDate = '" + nowDate + "' ORDER BY trnId DESC").list();
        if (turns.isEmpty()) {
            next = 1;
        } else {
            next = turns.get(0).getTrnNmbr()+ 1;
        }
        return next;
    }

    public Turns searchTurnById(Integer trnNmbr) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Turns) session.load(Turns.class, trnNmbr);
    }

    public Date mysqlDate(String fecha) {
        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
        String strFecha = fecha;
        Date fechaDate = null;
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            fechaDate = formato.parse(strFecha);
            return fechaDate;
        } catch (ParseException ex) {
            context.addMessage(null, new FacesMessage(ex.getMessage(), null));
            return fechaDate;
        }
    }
    
    public List<TurnsReasons> turnReasons(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("From TurnsReasons").list();
    }
    
    public TurnsReasons searchReasonById(int trsCode){
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (TurnsReasons) session.load(TurnsReasons.class, trsCode);
    }
}